<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <!-- <span v-if="$slots.default"><slot /></span> -->
    <span><slot /></span>
  </div>
</template>
<script>
// import { ref } from 'vue'
// import { toRef } from 'vue'
import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    // 获取父组件传递过来的modelValue的值
    const checked = useVModel(props, 'modelValue', emit)
    const changeChecked = () => {
      checked.value = !checked.value
      // emit('update:modelValue', !checked.value)
    }
    return { checked, changeChecked }

    // const checked = toRef(props, 'modelValue')
    // 获取父组件传递过来的modelValue的值
    // const changeChecked = () => {
    //   checked.value = !checked.value
    //   // emit('update:modelValue', !checked.value)
    // }
    // return { checked, changeChecked }
  }
}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
